<!DOCTYPE HTML>
<html lang="zh-cn" class="light" dir="ltr">
    <head>
            <script type="text/x-mathjax-config">
                MathJax.Hub.Config({
                    showProcessingMessages: false,
                    messageStyle: "none",
                    extensions: ['tex2jax.js'],
                    jax: ['input/TeX', 'output/HTML-CSS'],
                    tex2jax: {
                        inlineMath: [ ['$','$'], ["\\(","\\)"] ],
                        displayMath: [ ['$$','$$'], ["\\[","\\]"] ],
                        processEscapes: true
                    },
                    "HTML-CSS": { availableFonts: ["STIX", "TeX"] }
                });
            </script>
            
        <!-- Book generated using mdBook -->
        <meta charset="UTF-8">
        <title>类 - 仓颉编程语言扩展库 API</title>


        <!-- Custom HTML head -->
        
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="theme-color" content="#ffffff">

        <link rel="icon" href="../../../favicon.svg">
        <link rel="shortcut icon" href="../../../favicon.png">
        <link rel="stylesheet" href="../../../css/variables.css">
        <link rel="stylesheet" href="../../../css/general.css">
        <link rel="stylesheet" href="../../../css/chrome.css">
        <link rel="stylesheet" href="../../../css/print.css" media="print">

        <!-- Fonts -->
        <link rel="stylesheet" href="../../../FontAwesome/css/font-awesome.css">
        <link rel="stylesheet" href="../../../fonts/fonts.css">

        <!-- Highlight.js Stylesheets -->
        <link rel="stylesheet" href="../../../highlight.css">
        <link rel="stylesheet" href="../../../tomorrow-night.css">
        <link rel="stylesheet" href="../../../ayu-highlight.css">

        <!-- Custom theme stylesheets -->
        <link rel="stylesheet" href="../../../assets/style.css">
        <link rel="stylesheet" href="../../../assets/theme.css">

        <!-- MathJax -->
        <script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    </head>
    <body class="sidebar-visible no-js">
    <div id="body-container">
        <!-- Provide site root to javascript -->
        <script>
            var path_to_root = "../../../";
            var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "light" : "light";
        </script>

        <!-- Work around some values being stored in localStorage wrapped in quotes -->
        <script>
            try {
                var theme = localStorage.getItem('mdbook-theme');
                var sidebar = localStorage.getItem('mdbook-sidebar');

                if (theme.startsWith('"') && theme.endsWith('"')) {
                    localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
                }

                if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
                    localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
                }
            } catch (e) { }
        </script>

        <!-- Set the theme before any content is loaded, prevents flash -->
        <script>
            var theme;
            try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
            if (theme === null || theme === undefined) { theme = default_theme; }
            var html = document.querySelector('html');
            html.classList.remove('light')
            html.classList.add(theme);
            var body = document.querySelector('body');
            body.classList.remove('no-js')
            body.classList.add('js');
        </script>

        <input type="checkbox" id="sidebar-toggle-anchor" class="hidden">

        <!-- Hide / unhide sidebar before it is displayed -->
        <script>
            var body = document.querySelector('body');
            var sidebar = null;
            var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
            if (document.body.clientWidth >= 1080) {
                try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
                sidebar = sidebar || 'visible';
            } else {
                sidebar = 'hidden';
            }
            sidebar_toggle.checked = sidebar === 'visible';
            body.classList.remove('sidebar-visible');
            body.classList.add("sidebar-" + sidebar);
        </script>

        <nav id="sidebar" class="sidebar" aria-label="Table of contents">
            <div class="sidebar-scrollbox">
                <ol class="chapter"><li class="chapter-item expanded "><a href="../../../libs_overview.html"><strong aria-hidden="true">1.</strong> 仓颉编程语言扩展库 API</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../compress/zlib/zlib_package_overview.html"><strong aria-hidden="true">1.1.</strong> stdx.compress.zlib</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../compress/zlib/zlib_package_api/zlib_package_classes.html"><strong aria-hidden="true">1.1.1.</strong> 类</a></li><li class="chapter-item "><a href="../../../compress/zlib/zlib_package_api/zlib_package_enums.html"><strong aria-hidden="true">1.1.2.</strong> 枚举</a></li><li class="chapter-item "><a href="../../../compress/zlib/zlib_package_api/zlib_package_exceptions.html"><strong aria-hidden="true">1.1.3.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.1.4.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../compress/zlib/zlib_samples/deflate_compress_decompress.html"><strong aria-hidden="true">1.1.4.1.</strong> Deflate 格式数据的压缩和解压</a></li><li class="chapter-item "><a href="../../../compress/zlib/zlib_samples/gzip_compress_decompress.html"><strong aria-hidden="true">1.1.4.2.</strong> Gzip 格式数据的压缩和解压</a></li></ol></li></ol></li><li class="chapter-item expanded "><a href="../../../crypto/crypto/crypto_package_overview.html"><strong aria-hidden="true">1.2.</strong> stdx.crypto.crypto</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item expanded "><a href="../../../crypto/crypto/crypto_package_api/crypto_package_classes.html" class="active"><strong aria-hidden="true">1.2.1.</strong> 类</a></li><li class="chapter-item "><a href="../../../crypto/crypto/crypto_package_api/crypto_package_structs.html"><strong aria-hidden="true">1.2.2.</strong> 结构体</a></li><li class="chapter-item "><a href="../../../crypto/crypto/crypto_package_api/crypto_package_exceptions.html"><strong aria-hidden="true">1.2.3.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.2.4.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../crypto/crypto/crypto_samples/sample_secure_random.html"><strong aria-hidden="true">1.2.4.1.</strong> SecureRandom 使用</a></li><li class="chapter-item "><a href="../../../crypto/crypto/crypto_samples/sample_crypto.html"><strong aria-hidden="true">1.2.4.2.</strong> SM4 使用</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../crypto/digest/crypto_digest_package_overview.html"><strong aria-hidden="true">1.3.</strong> stdx.crypto.digest</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../crypto/digest/digest_package_api/digest_package_classes.html"><strong aria-hidden="true">1.3.1.</strong> 类</a></li><li class="chapter-item "><a href="../../../crypto/digest/digest_package_api/digest_package_structs.html"><strong aria-hidden="true">1.3.2.</strong> 结构体</a></li><li class="chapter-item "><a href="../../../crypto/digest/digest_package_api/digest_package_exceptions.html"><strong aria-hidden="true">1.3.3.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.3.4.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../crypto/digest/digest_samples/sample_digest.html"><strong aria-hidden="true">1.3.4.1.</strong> digest 使用</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../crypto/keys/keys_package_overview.html"><strong aria-hidden="true">1.4.</strong> stdx.crypto.keys</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../crypto/keys/keys_package_api/keys_package_classes.html"><strong aria-hidden="true">1.4.1.</strong> 类</a></li><li class="chapter-item "><a href="../../../crypto/keys/keys_package_api/keys_package_enums.html"><strong aria-hidden="true">1.4.2.</strong> 枚举</a></li><li class="chapter-item "><a href="../../../crypto/keys/keys_package_api/keys_package_structs.html"><strong aria-hidden="true">1.4.3.</strong> 结构体</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.4.4.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../crypto/keys/keys_samples/sample_keys.html"><strong aria-hidden="true">1.4.4.1.</strong> keys 使用</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../crypto/x509/x509_package_overview.html"><strong aria-hidden="true">1.5.</strong> stdx.crypto.x509</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../crypto/x509/x509_package_api/x509_package_type.html"><strong aria-hidden="true">1.5.1.</strong> 类型别名</a></li><li class="chapter-item "><a href="../../../crypto/x509/x509_package_api/x509_package_interfaces.html"><strong aria-hidden="true">1.5.2.</strong> 接口</a></li><li class="chapter-item "><a href="../../../crypto/x509/x509_package_api/x509_package_classes.html"><strong aria-hidden="true">1.5.3.</strong> 类</a></li><li class="chapter-item "><a href="../../../crypto/x509/x509_package_api/x509_package_enums.html"><strong aria-hidden="true">1.5.4.</strong> 枚举</a></li><li class="chapter-item "><a href="../../../crypto/x509/x509_package_api/x509_package_structs.html"><strong aria-hidden="true">1.5.5.</strong> 结构体</a></li><li class="chapter-item "><a href="../../../crypto/x509/x509_package_api/x509_package_exceptions.html"><strong aria-hidden="true">1.5.6.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.5.7.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../crypto/x509/x509_samples/sample_x509.html"><strong aria-hidden="true">1.5.7.1.</strong> x509 使用</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../encoding/base64/base64_package_overview.html"><strong aria-hidden="true">1.6.</strong> stdx.encoding.base64</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/base64/base64_package_api/base64_package_funcs.html"><strong aria-hidden="true">1.6.1.</strong> 函数</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.6.2.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/base64/base64_samples/base64.html"><strong aria-hidden="true">1.6.2.1.</strong> Byte 数组和 Base64 互转</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../encoding/hex/hex_package_overview.html"><strong aria-hidden="true">1.7.</strong> stdx.encoding.hex</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/hex/hex_package_api/hex_package_funcs.html"><strong aria-hidden="true">1.7.1.</strong> 函数</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.7.2.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/hex/hex_samples/hex.html"><strong aria-hidden="true">1.7.2.1.</strong> Byte 数组和 Hex 互转</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../encoding/json/json_package_overview.html"><strong aria-hidden="true">1.8.</strong> stdx.encoding.json</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/json/json_package_api/encoding_json_package_interfaces.html"><strong aria-hidden="true">1.8.1.</strong> 接口</a></li><li class="chapter-item "><a href="../../../encoding/json/json_package_api/encoding_json_package_classes.html"><strong aria-hidden="true">1.8.2.</strong> 类</a></li><li class="chapter-item "><a href="../../../encoding/json/json_package_api/encoding_json_package_enums.html"><strong aria-hidden="true">1.8.3.</strong> 枚举</a></li><li class="chapter-item "><a href="../../../encoding/json/json_package_api/encoding_json_package_exceptions.html"><strong aria-hidden="true">1.8.4.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.8.5.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/json/json_samples/json_array_sample.html"><strong aria-hidden="true">1.8.5.1.</strong> JsonArray 使用示例</a></li><li class="chapter-item "><a href="../../../encoding/json/json_samples/json_value_sample.html"><strong aria-hidden="true">1.8.5.2.</strong> JsonValue 和 String 互相转换</a></li><li class="chapter-item "><a href="../../../encoding/json/json_samples/to_json_sample.html"><strong aria-hidden="true">1.8.5.3.</strong> JsonValue 与 DataModel 的转换</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_package_overview.html"><strong aria-hidden="true">1.9.</strong> stdx.encoding.json.stream</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_package_api/encoding_json_stream_package_interfaces.html"><strong aria-hidden="true">1.9.1.</strong> 接口</a></li><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_package_api/encoding_json_stream_package_classes.html"><strong aria-hidden="true">1.9.2.</strong> 类</a></li><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_package_api/encoding_json_stream_package_enums.html"><strong aria-hidden="true">1.9.3.</strong> 枚举</a></li><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_package_api/encoding_json_stream_package_structs.html"><strong aria-hidden="true">1.9.4.</strong> 结构体</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.9.5.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_samples/sample_json_reader.html"><strong aria-hidden="true">1.9.5.1.</strong> 使用 Json Stream 进行反序列化</a></li><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_samples/sample_json_writer.html"><strong aria-hidden="true">1.9.5.2.</strong> 使用 Json Stream 进行序列化</a></li><li class="chapter-item "><a href="../../../encoding/json_stream/json_stream_samples/sample_json_writeconfig.html"><strong aria-hidden="true">1.9.5.3.</strong> WriteConfig 使用示例</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../encoding/url/url_package_overview.html"><strong aria-hidden="true">1.10.</strong> stdx.encoding.url</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/url/url_package_api/url_package_classes.html"><strong aria-hidden="true">1.10.1.</strong> 类</a></li><li class="chapter-item "><a href="../../../encoding/url/url_package_api/url_package_exceptions.html"><strong aria-hidden="true">1.10.2.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.10.3.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../encoding/url/url_samples/form.html"><strong aria-hidden="true">1.10.3.1.</strong> Form 的构造使用</a></li><li class="chapter-item "><a href="../../../encoding/url/url_samples/url_parse.html"><strong aria-hidden="true">1.10.3.2.</strong> URL 解析函数 parse 的使用</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../fuzz/fuzz_package_overview.html"><strong aria-hidden="true">1.11.</strong> stdx.fuzz.fuzz</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../fuzz/fuzz_package_api/fuzz_package_constants_vars.html"><strong aria-hidden="true">1.11.1.</strong> 常量变量</a></li><li class="chapter-item "><a href="../../../fuzz/fuzz_package_api/fuzz_package_classes.html"><strong aria-hidden="true">1.11.2.</strong> 类</a></li><li class="chapter-item "><a href="../../../fuzz/fuzz_package_api/fuzz_package_exceptions.html"><strong aria-hidden="true">1.11.3.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.11.4.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../fuzz/fuzz_samples/basic_fuzzing_test.html"><strong aria-hidden="true">1.11.4.1.</strong> 测试猜测字符功能</a></li><li class="chapter-item "><a href="../../../fuzz/fuzz_samples/dataprovider_usage.html"><strong aria-hidden="true">1.11.4.2.</strong> 使用 DataProvider 功能进行测试</a></li><li class="chapter-item "><a href="../../../fuzz/fuzz_samples/fake_coverage_usage.html"><strong aria-hidden="true">1.11.4.3.</strong> 使用 FakeCoverage 避免 DataProvider 模式下 Fuzz 异常终止</a></li><li class="chapter-item "><a href="../../../fuzz/fuzz_samples/print_cj-fuzz_usage.html"><strong aria-hidden="true">1.11.4.4.</strong> 打印 fuzz 使用方法</a></li><li class="chapter-item "><a href="../../../fuzz/fuzz_samples/print_coverage.html"><strong aria-hidden="true">1.11.4.5.</strong> 实验性特性-覆盖率信息打印</a></li><li class="chapter-item "><a href="../../../fuzz/fuzz_samples/stack_backtrace_missing_solution.html"><strong aria-hidden="true">1.11.4.6.</strong> 栈回溯缺失的处理方案</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../log/log_package_overview.html"><strong aria-hidden="true">1.12.</strong> stdx.log</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../log/log_package_api/log_package_types.html"><strong aria-hidden="true">1.12.1.</strong> 类型别名</a></li><li class="chapter-item "><a href="../../../log/log_package_api/log_package_funcs.html"><strong aria-hidden="true">1.12.2.</strong> 函数</a></li><li class="chapter-item "><a href="../../../log/log_package_api/log_package_interfaces.html"><strong aria-hidden="true">1.12.3.</strong> 接口</a></li><li class="chapter-item "><a href="../../../log/log_package_api/log_package_classes.html"><strong aria-hidden="true">1.12.4.</strong> 类</a></li><li class="chapter-item "><a href="../../../log/log_package_api/log_package_structs.html"><strong aria-hidden="true">1.12.5.</strong> 结构体</a></li><li class="chapter-item "><a href="../../../log/log_package_api/log_package_exceptions.html"><strong aria-hidden="true">1.12.6.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.12.7.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../log/log_samples/log_sample.html"><strong aria-hidden="true">1.12.7.1.</strong> 日志打印示例</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../logger/logger_package_overview.html"><strong aria-hidden="true">1.13.</strong> stdx.logger</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../logger/logger_package_api/logger_package_classes.html"><strong aria-hidden="true">1.13.1.</strong> 类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.13.2.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../logger/logger_samples/logger_sample.html"><strong aria-hidden="true">1.13.2.1.</strong> 日志打印示例</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../net/http/http_package_overview.html"><strong aria-hidden="true">1.14.</strong> stdx.net.http</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../net/http/http_package_api/http_package_funcs.html"><strong aria-hidden="true">1.14.1.</strong> 函数</a></li><li class="chapter-item "><a href="../../../net/http/http_package_api/http_package_interfaces.html"><strong aria-hidden="true">1.14.2.</strong> 接口</a></li><li class="chapter-item "><a href="../../../net/http/http_package_api/http_package_classes.html"><strong aria-hidden="true">1.14.3.</strong> 类</a></li><li class="chapter-item "><a href="../../../net/http/http_package_api/http_package_enums.html"><strong aria-hidden="true">1.14.4.</strong> 枚举</a></li><li class="chapter-item "><a href="../../../net/http/http_package_api/http_package_structs.html"><strong aria-hidden="true">1.14.5.</strong> 结构体</a></li><li class="chapter-item "><a href="../../../net/http/http_package_api/http_package_exceptions.html"><strong aria-hidden="true">1.14.6.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.14.7.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../net/http/http_samples/http_client.html"><strong aria-hidden="true">1.14.7.1.</strong> client</a></li><li class="chapter-item "><a href="../../../net/http/http_samples/cookie.html"><strong aria-hidden="true">1.14.7.2.</strong> cookie</a></li><li class="chapter-item "><a href="../../../net/http/http_samples/log.html"><strong aria-hidden="true">1.14.7.3.</strong> log</a></li><li class="chapter-item "><a href="../../../net/http/http_samples/http_server.html"><strong aria-hidden="true">1.14.7.4.</strong> server</a></li><li class="chapter-item "><a href="../../../net/http/http_samples/webSocket.html"><strong aria-hidden="true">1.14.7.5.</strong> webSocket</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../net/tls/tls_package_overview.html"><strong aria-hidden="true">1.15.</strong> stdx.net.tls</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../net/tls/tls_package_api/tls_package_classes.html"><strong aria-hidden="true">1.15.1.</strong> 类</a></li><li class="chapter-item "><a href="../../../net/tls/tls_package_api/tls_package_enums.html"><strong aria-hidden="true">1.15.2.</strong> 枚举</a></li><li class="chapter-item "><a href="../../../net/tls/tls_package_api/tls_package_structs.html"><strong aria-hidden="true">1.15.3.</strong> 结构体</a></li><li class="chapter-item "><a href="../../../net/tls/tls_package_api/tls_package_exceptions.html"><strong aria-hidden="true">1.15.4.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.15.5.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../net/tls/tls_samples/cert_key.html"><strong aria-hidden="true">1.15.5.1.</strong> 服务端证书及公钥在一份文件中</a></li><li class="chapter-item "><a href="../../../net/tls/tls_samples/client.html"><strong aria-hidden="true">1.15.5.2.</strong> 客户端示例</a></li><li class="chapter-item "><a href="../../../net/tls/tls_samples/hot_update_cert.html"><strong aria-hidden="true">1.15.5.3.</strong> 证书热更新</a></li><li class="chapter-item "><a href="../../../net/tls/tls_samples/server.html"><strong aria-hidden="true">1.15.5.4.</strong> 服务端示例</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../serialization/serialization_package_overview.html"><strong aria-hidden="true">1.16.</strong> stdx.serialization.serialization</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../serialization/serialization_package_api/serialization_package_functions.html"><strong aria-hidden="true">1.16.1.</strong> 函数</a></li><li class="chapter-item "><a href="../../../serialization/serialization_package_api/serialization_package_interfaces.html"><strong aria-hidden="true">1.16.2.</strong> 接口</a></li><li class="chapter-item "><a href="../../../serialization/serialization_package_api/serialization_package_classes.html"><strong aria-hidden="true">1.16.3.</strong> 类</a></li><li class="chapter-item "><a href="../../../serialization/serialization_package_api/serialization_package_exceptions.html"><strong aria-hidden="true">1.16.4.</strong> 异常类</a></li><li class="chapter-item "><div><strong aria-hidden="true">1.16.5.</strong> 示例教程</div><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../serialization/serialization_samples/serialize_and_deserialize_class.html"><strong aria-hidden="true">1.16.5.1.</strong> class 序列化和反序列化</a></li><li class="chapter-item "><a href="../../../serialization/serialization_samples/serialize_hashmap_and_hashset.html"><strong aria-hidden="true">1.16.5.2.</strong> HashSet 和 HashMap 序列化</a></li></ol></li></ol></li><li class="chapter-item "><a href="../../../unittest/data/data_package_overview.html"><strong aria-hidden="true">1.17.</strong> stdx.unittest.data</a><a class="toggle"><div>❱</div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../../unittest/data/data_package_api/data_package_functions.html"><strong aria-hidden="true">1.17.1.</strong> 函数</a></li><li class="chapter-item "><a href="../../../unittest/data/data_package_api/data_package_classes.html"><strong aria-hidden="true">1.17.2.</strong> 类</a></li></ol></li></ol></li></ol>
            </div>
            <div id="sidebar-resize-handle" class="sidebar-resize-handle">
                <div class="sidebar-resize-indicator"></div>
            </div>
        </nav>

        <!-- Track and set sidebar scroll position -->
        <script>
            var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
            sidebarScrollbox.addEventListener('click', function(e) {
                if (e.target.tagName === 'A') {
                    sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
                }
            }, { passive: true });
            var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
            sessionStorage.removeItem('sidebar-scroll');
            if (sidebarScrollTop) {
                // preserve sidebar scroll position when navigating via links within sidebar
                sidebarScrollbox.scrollTop = sidebarScrollTop;
            } else {
                // scroll sidebar to current active section when navigating via "next/previous chapter" buttons
                var activeSection = document.querySelector('#sidebar .active');
                if (activeSection) {
                    activeSection.scrollIntoView({ block: 'center' });
                }
            }
        </script>

        <div id="page-wrapper" class="page-wrapper">

            <div class="page">
                                <div id="menu-bar-hover-placeholder"></div>
                <div id="menu-bar" class="menu-bar sticky">
                    <div class="left-buttons">
                        <label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
                            <i class="fa fa-bars"></i>
                        </label>
                        <button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
                            <i class="fa fa-paint-brush"></i>
                        </button>
                        <ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
                            <li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
                            <li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
                        </ul>
                        <button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>

                    <h1 class="menu-title">仓颉编程语言扩展库 API</h1>

                    <div class="right-buttons">
                        <a href="../../../print.html" title="Print this book" aria-label="Print this book">
                            <i id="print-button" class="fa fa-print"></i>
                        </a>

                    </div>
                </div>

                <div id="search-wrapper" class="hidden">
                    <form id="searchbar-outer" class="searchbar-outer">
                        <input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
                    </form>
                    <div id="searchresults-outer" class="searchresults-outer hidden">
                        <div id="searchresults-header" class="searchresults-header"></div>
                        <ul id="searchresults">
                        </ul>
                    </div>
                </div>

                <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
                <script>
                    document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
                    document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
                    Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
                        link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
                    });
                </script>

                <div id="content" class="content">
                    <main>
                        <h1 id="类"><a class="header" href="#类">类</a></h1>
<h2 id="class-securerandom"><a class="header" href="#class-securerandom">class SecureRandom</a></h2>
<pre><code class="language-cangjie">public class SecureRandom {
    public init(priv!: Bool = false)
}
</code></pre>
<p>功能：用于生成加密安全的伪随机数。</p>
<p>和 Random 相比，主要有三个方面不同：</p>
<ul>
<li>
<p>随机数种子： Random 使用系统时钟作为默认的种子，时间戳一样，结果就相同；<a href="crypto_package_classes.html#class-securerandom">SecureRandom</a> 使用操作系统或者硬件提供的随机数种子，生成的是真随机数。</p>
</li>
<li>
<p>随机数生成： Random 使用了梅森旋转伪随机生成器；<a href="crypto_package_classes.html#class-securerandom">SecureRandom</a> 则使用了 openssl 库提供的 <a href="../../digest/digest_package_api/digest_package_classes.html#class-md5">MD5</a> 等随机算法，使用熵源生成真随机数；如果硬件支持，还可以使用硬件随机数生成器来生成安全性更强的随机数。</p>
</li>
<li>
<p>安全性： Random 不能用于加密安全的应用或者隐私数据的保护，可以使用 <a href="crypto_package_classes.html#class-securerandom">SecureRandom</a>。</p>
</li>
</ul>
<p>使用示例见 <a href="../crypto_samples/sample_secure_random.html#SecureRandom-%E4%BD%BF%E7%94%A8">SecureRandom 使用</a>。</p>
<h3 id="initbool"><a class="header" href="#initbool">init(Bool)</a></h3>
<pre><code class="language-cangjie">public init(priv!: Bool = false)
</code></pre>
<p>功能：创建 <a href="crypto_package_classes.html#class-securerandom">SecureRandom</a> 实例，可指定是否使用更加安全的加密安全伪随机生成器，加密安全伪随机生成器可用于会话密钥和证书私钥等加密场景。</p>
<p>参数：</p>
<ul>
<li>priv!: Bool - 设置为 true 表示使用加密安全伪随机生成器。</li>
</ul>
<h3 id="func-nextbitsuint64"><a class="header" href="#func-nextbitsuint64">func nextBits(UInt64)</a></h3>
<pre><code class="language-cangjie">public func nextBits(bits: UInt64): UInt64
</code></pre>
<p>功能：生成一个指定位长的随机整数。</p>
<p>参数:</p>
<ul>
<li>bits: UInt64 - 要生成的随机数的位数，取值范围 (0, 64]。</li>
</ul>
<p>返回值：</p>
<ul>
<li>UInt64 - 生成的用户指定位长的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 如果 <code>bits</code> 等于 0，或大于 64，超过所能截取的 UInt64 长度，则抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextbool"><a class="header" href="#func-nextbool">func nextBool()</a></h3>
<pre><code class="language-cangjie">public func nextBool(): Bool
</code></pre>
<p>功能：获取一个随机的 Bool 类型实例。</p>
<p>返回值：</p>
<ul>
<li>Bool - 一个随机的 Bool 类型实例。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextbytesarraybyte"><a class="header" href="#func-nextbytesarraybyte">func nextBytes(Array&lt;Byte&gt;)</a></h3>
<pre><code class="language-cangjie">public func nextBytes(bytes: Array&lt;Byte&gt;): Unit
</code></pre>
<p>功能：生成随机数替换入参数组中的每个元素。</p>
<p>参数：</p>
<ul>
<li>bytes: Array&lt;Byte&gt; - 被替换的数组。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextbytesint32"><a class="header" href="#func-nextbytesint32">func nextBytes(Int32)</a></h3>
<pre><code class="language-cangjie">public func nextBytes(length: Int32): Array&lt;Byte&gt;
</code></pre>
<p>功能：获取一个指定长度的随机字节的数组。</p>
<p>参数：</p>
<ul>
<li>length: Int32 - 要生成的随机字节数组的长度。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Array&lt;Byte&gt; - 一个随机字节数组。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当参数 length 小于等于 0，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextfloat16"><a class="header" href="#func-nextfloat16">func nextFloat16()</a></h3>
<pre><code class="language-cangjie">public func nextFloat16(): Float16
</code></pre>
<p>功能：获取一个 Float16 类型且在区间 [0.0, 1.0) 内的随机数。</p>
<p>返回值：</p>
<ul>
<li>Float16 - 一个 Float16 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextfloat32"><a class="header" href="#func-nextfloat32">func nextFloat32()</a></h3>
<pre><code class="language-cangjie">public func nextFloat32(): Float32
</code></pre>
<p>功能：获取一个 Float32 类型且在区间 [0.0, 1.0) 内的随机数。</p>
<p>返回值：</p>
<ul>
<li>Float32 - 一个 Float32 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextfloat64"><a class="header" href="#func-nextfloat64">func nextFloat64()</a></h3>
<pre><code class="language-cangjie">public func nextFloat64(): Float64
</code></pre>
<p>功能：获取一个 Float64 类型且在区间 [0.0, 1.0) 内的随机数。</p>
<p>返回值：</p>
<ul>
<li>Float64 - 一个 Float64 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextgaussianfloat16float16-float16"><a class="header" href="#func-nextgaussianfloat16float16-float16">func nextGaussianFloat16(Float16, Float16)</a></h3>
<pre><code class="language-cangjie">public func nextGaussianFloat16(mean!: Float16 = 0.0, sigma!: Float16 = 1.0): Float16
</code></pre>
<p>功能：默认获取一个 Float16 类型且符合均值为 0.0 标准差为 1.0 的高斯分布的随机数，其中均值是期望值，可解释为位置参数，决定了分布的位置，标准差可解释为尺度参数，决定了分布的幅度。</p>
<p>参数：</p>
<ul>
<li>mean!: Float16 - 均值。</li>
<li>sigma!: Float16 - 标准差。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Float16 - 一个 Float16 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextgaussianfloat32float32-float32"><a class="header" href="#func-nextgaussianfloat32float32-float32">func nextGaussianFloat32(Float32, Float32)</a></h3>
<pre><code class="language-cangjie">public func nextGaussianFloat32(mean!: Float32 = 0.0, sigma!: Float32 = 1.0): Float32
</code></pre>
<p>功能：默认获取一个 Float32 类型且符合均值为 0.0 标准差为 1.0 的高斯分布的随机数，其中均值是期望值，可解释为位置参数，决定了分布的位置，标准差可解释为尺度参数，决定了分布的幅度。</p>
<p>参数：</p>
<ul>
<li>mean!: Float32 - 均值。</li>
<li>sigma!: Float32 - 标准差。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Float32 - 一个 Float32 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextgaussianfloat64float64-float64"><a class="header" href="#func-nextgaussianfloat64float64-float64">func nextGaussianFloat64(Float64, Float64)</a></h3>
<pre><code class="language-cangjie">public func nextGaussianFloat64(mean!: Float64 = 0.0, sigma!: Float64 = 1.0): Float64
</code></pre>
<p>功能：默认获取一个 Float64 类型且符合均值为 0.0 标准差为 1.0 的高斯分布的随机数，其中均值是期望值，可解释为位置参数，决定了分布的位置，标准差可解释为尺度参数，决定了分布的幅度。</p>
<p>参数：</p>
<ul>
<li>mean!: Float64 - 均值。</li>
<li>sigma!: Float64 - 标准差。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Float64 - 一个 Float64 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint16"><a class="header" href="#func-nextint16">func nextInt16()</a></h3>
<pre><code class="language-cangjie">public func nextInt16(): Int16
</code></pre>
<p>功能：获取一个 Int16 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>Int16 - 一个 Int16 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint32"><a class="header" href="#func-nextint32">func nextInt32()</a></h3>
<pre><code class="language-cangjie">public func nextInt32(): Int32
</code></pre>
<p>功能：获取一个 Int32 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>Int32 - 一个 Int32 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint16int16"><a class="header" href="#func-nextint16int16">func nextInt16(Int16)</a></h3>
<pre><code class="language-cangjie">public func nextInt16(max: Int16): Int16
</code></pre>
<p>功能：获取一个 Int16 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: Int16 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Int16 - 一个 Int16 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为非正数时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint32int32"><a class="header" href="#func-nextint32int32">func nextInt32(Int32)</a></h3>
<pre><code class="language-cangjie">public func nextInt32(max: Int32): Int32
</code></pre>
<p>功能：获取一个 Int32 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: Int32 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Int32 - 一个 Int32 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为非正数时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint64"><a class="header" href="#func-nextint64">func nextInt64()</a></h3>
<pre><code class="language-cangjie">public func nextInt64(): Int64
</code></pre>
<p>功能：获取一个 Int64 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>Int64 - 一个 Int64 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint64int64"><a class="header" href="#func-nextint64int64">func nextInt64(Int64)</a></h3>
<pre><code class="language-cangjie">public func nextInt64(max: Int64): Int64
</code></pre>
<p>功能：获取一个 Int64 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: Int64 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Int64 - 一个 Int64 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为非正数时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint8"><a class="header" href="#func-nextint8">func nextInt8()</a></h3>
<pre><code class="language-cangjie">public func nextInt8(): Int8
</code></pre>
<p>功能：获取一个 Int8 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>Int8 - 一个 Int8 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextint8int8"><a class="header" href="#func-nextint8int8">func nextInt8(Int8)</a></h3>
<pre><code class="language-cangjie">public func nextInt8(max: Int8): Int8
</code></pre>
<p>功能：获取一个 Int8 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: Int8 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Int8 - 一个 Int8 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为非正数时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint16"><a class="header" href="#func-nextuint16">func nextUInt16()</a></h3>
<pre><code class="language-cangjie">public func nextUInt16(): UInt16
</code></pre>
<p>功能：获取一个 UInt16 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>UInt16 - 一个 UInt16 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint16uint16"><a class="header" href="#func-nextuint16uint16">func nextUInt16(UInt16)</a></h3>
<pre><code class="language-cangjie">public func nextUInt16(max: UInt16): UInt16
</code></pre>
<p>功能：获取一个 UInt16 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: UInt16 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>UInt16 - 一个 UInt16 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为 0 时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint32"><a class="header" href="#func-nextuint32">func nextUInt32()</a></h3>
<pre><code class="language-cangjie">public func nextUInt32(): UInt32
</code></pre>
<p>功能：获取一个 UInt32 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>UInt32 - 一个 UInt32 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint32uint32"><a class="header" href="#func-nextuint32uint32">func nextUInt32(UInt32)</a></h3>
<pre><code class="language-cangjie">public func nextUInt32(max: UInt32): UInt32
</code></pre>
<p>功能：获取一个 UInt32 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: UInt32 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>UInt32 - 一个 UInt32 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为 0 时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint64"><a class="header" href="#func-nextuint64">func nextUInt64()</a></h3>
<pre><code class="language-cangjie">public func nextUInt64(): UInt64
</code></pre>
<p>功能：获取一个 UInt64 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>UInt64 - 一个 UInt64 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint64uint64"><a class="header" href="#func-nextuint64uint64">func nextUInt64(UInt64)</a></h3>
<pre><code class="language-cangjie">public func nextUInt64(max: UInt64): UInt64
</code></pre>
<p>功能：获取一个 UInt64 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: UInt64 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>UInt64 - 一个 UInt64 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为 0 时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint8"><a class="header" href="#func-nextuint8">func nextUInt8()</a></h3>
<pre><code class="language-cangjie">public func nextUInt8(): UInt8
</code></pre>
<p>功能：获取一个 UInt8 类型的随机数。</p>
<p>返回值：</p>
<ul>
<li>UInt8 - 一个 UInt8 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h3 id="func-nextuint8uint8"><a class="header" href="#func-nextuint8uint8">func nextUInt8(UInt8)</a></h3>
<pre><code class="language-cangjie">public func nextUInt8(max: UInt8): UInt8
</code></pre>
<p>功能：获取一个 UInt8 类型且在区间 [0, max) 内的随机数。</p>
<p>参数：</p>
<ul>
<li>max: UInt8 - 区间最大值。</li>
</ul>
<p>返回值：</p>
<ul>
<li>UInt8 - 一个 UInt8 类型的随机数。</li>
</ul>
<p>异常：</p>
<ul>
<li>IllegalArgumentException - 当 max 为 0 时，抛出异常。</li>
<li><a href="crypto_package_exceptions.html#class-securerandomexception">SecureRandomException</a> - 当生成器不能正确生成随机数或生成随机数失败时，抛出异常。</li>
</ul>
<h2 id="class-sm4"><a class="header" href="#class-sm4">class SM4</a></h2>
<pre><code class="language-cangjie">public class SM4 &lt;: BlockCipher {
    public init(
        optMode: OperationMode,
        key: Array&lt;Byte&gt;,
        iv!: Array&lt;Byte&gt; = Array&lt;Byte&gt;(),
        paddingMode!: PaddingMode = PaddingMode.PKCS7Padding,
        aad!: Array&lt;Byte&gt; = Array&lt;Byte&gt;(),
        tagSize!: Int64 = 16
    )
}
</code></pre>
<p>功能：提供国密SM4对称加解密。</p>
<p>目前 SM4 支持 的加解密工作模式由 <a href="crypto_package_structs.html#struct-operationmode">OperationMode</a> 定义，目前支持 ECB、CBC、OFB、CFB、CTR、GCM模式。</p>
<p>不同的工作模式可能对应的加解密实现不同，安全性也不同。需要选择和场景适配的加解密工作模式。</p>
<p>iv 初始化向量在 GCM 模式下可以设置推荐长度是12字节，在 CBC、OFB、CFB、CTR 模式下 iv 长度是16字节，在 ECB 模式下 iv 可以不设置。</p>
<p>paddingMode 填充模式模式由 <a href="crypto_package_structs.html#struct-paddingmode">PaddingMode</a> 定义， 目前支持 NoPadding 非填充、PKCS7Padding PKCS7填充。默认是 PKCS7 填充。</p>
<p>paddingMode 设置对ECB 和 CBC 有效，ECB 和 CBC 分组加解密需要分组长度等于 blockSize。会根据填充模式进行填充。 paddingMode 设置对 OFB、CFB、CTR、GCM 工作模式无效，这些工作模式均无需填充。</p>
<p>如果选择 NoPadding 模式，即不填充。则在 ECB 和 CBC 工作模式下用户需要对数据是否可以分组负责，如果数据不能分组，或者最后一组数据长度不足 blockSize 则会报错。</p>
<p>aad 附加数据，仅在 GCM 模式下使用，由用户填充，参与摘要计算，默认为空。</p>
<p>tagSize 设置摘要长度，仅在 GCM 模式下使用，默认值为 SM4_GCM_TAG_SIZE 16字节，最小不能低于12字节，最大不能超过16字节。</p>
<p>如果是 GCM 工作模式。加密结果的后 tagSize 字节是摘要数据。</p>
<p>使用示例见 <a href="../crypto_samples/sample_crypto.html#SM4-%E4%BD%BF%E7%94%A8">SM4 使用</a>。</p>
<blockquote>
<p><strong>注意：</strong></p>
<p>GCM 模式需要 OpenSSL 3.2 或者以上版本。</p>
</blockquote>
<p>父类型：</p>
<ul>
<li>BlockCipher</li>
</ul>
<h3 id="initoperationmode-arraybyte-arraybyte-paddingmode-arraybyte-int64"><a class="header" href="#initoperationmode-arraybyte-arraybyte-paddingmode-arraybyte-int64">init(OperationMode, Array&lt;Byte&gt;, Array&lt;Byte&gt;, PaddingMode, Array&lt;Byte&gt;, Int64)</a></h3>
<pre><code class="language-cangjie">    public init(
        optMode: OperationMode,
        key: Array&lt;Byte&gt;,
        iv!: Array&lt;Byte&gt; = Array&lt;Byte&gt;(),
        paddingMode!: PaddingMode = PaddingMode.PKCS7Padding,
        aad!: Array&lt;Byte&gt; = Array&lt;Byte&gt;(),
        tagSize!: Int64 = 16
    )
</code></pre>
<p>功能：创建 <a href="crypto_package_classes.html#class-sm4">SM4</a> 实例，可指定在不同工作模式下参数。</p>
<p>参数：</p>
<ul>
<li>optMode: <a href="crypto_package_structs.html#struct-operationmode">OperationMode</a> - 设置加解密工作模式。</li>
<li>key: Array&lt;Byte&gt; - 密钥，长度为16字节。</li>
<li>iv!: Array&lt;Byte&gt; - 初始化向量。</li>
<li>paddingMode!: <a href="crypto_package_structs.html#struct-paddingmode">PaddingMode</a> - 设置填充模式。</li>
<li>aad!: Array&lt;Byte&gt; - 设置附加数据。</li>
<li>tagSize!: Int64 - 设置摘要长度。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="../../digest/digest_package_api/digest_package_exceptions.html#class-cryptoexception">CryptoException</a> - 参数设置不正确，实例化失败。</li>
</ul>
<h3 id="prop-aad"><a class="header" href="#prop-aad">prop aad</a></h3>
<pre><code class="language-cangjie">public prop aad: Array&lt;Byte&gt;
</code></pre>
<p>功能：附加数据。</p>
<p>类型：Array&lt;Byte&gt;</p>
<h3 id="prop-algorithm"><a class="header" href="#prop-algorithm">prop algorithm</a></h3>
<pre><code class="language-cangjie">public prop algorithm: String
</code></pre>
<p>功能：获取分组加解密算法的算法名称。</p>
<p>类型：String</p>
<h3 id="prop-blocksize"><a class="header" href="#prop-blocksize">prop blockSize</a></h3>
<pre><code class="language-cangjie">public prop blockSize: Int64
</code></pre>
<p>功能：分组长度，单位字节。</p>
<p>类型：Int64</p>
<h3 id="prop-keysize"><a class="header" href="#prop-keysize">prop keySize</a></h3>
<pre><code class="language-cangjie">public prop keySize: Int64
</code></pre>
<p>功能：密钥长度。</p>
<p>类型：Int64</p>
<h3 id="prop-key"><a class="header" href="#prop-key">prop key</a></h3>
<pre><code class="language-cangjie">public prop key: Array&lt;Byte&gt;
</code></pre>
<p>功能：密钥。</p>
<p>类型：Array&lt;Byte&gt;</p>
<h3 id="prop-optmode"><a class="header" href="#prop-optmode">prop optMode</a></h3>
<pre><code class="language-cangjie">public prop optMode: OperationMode
</code></pre>
<p>功能：工作模式。</p>
<p>类型：<a href="crypto_package_structs.html#struct-operationmode">OperationMode</a></p>
<h3 id="prop-paddingmode"><a class="header" href="#prop-paddingmode">prop paddingMode</a></h3>
<pre><code class="language-cangjie">public prop paddingMode: PaddingMode
</code></pre>
<p>功能：填充模式。</p>
<p>类型：<a href="crypto_package_structs.html#struct-paddingmode">PaddingMode</a></p>
<h3 id="prop-iv"><a class="header" href="#prop-iv">prop iv</a></h3>
<pre><code class="language-cangjie">public prop iv: Array&lt;Byte&gt;
</code></pre>
<p>功能：初始化向量。</p>
<p>类型：Array&lt;Byte&gt;</p>
<h3 id="prop-ivsize"><a class="header" href="#prop-ivsize">prop ivSize</a></h3>
<pre><code class="language-cangjie">public prop ivSize: Int64
</code></pre>
<p>功能：初始化向量长度。</p>
<p>类型：Int64</p>
<h3 id="prop-tagsize"><a class="header" href="#prop-tagsize">prop tagSize</a></h3>
<pre><code class="language-cangjie">public prop tagSize: Int64
</code></pre>
<p>功能：摘要长度。</p>
<p>类型：Int64</p>
<h3 id="func-encryptarraybyte"><a class="header" href="#func-encryptarraybyte">func encrypt(Array&lt;Byte&gt;)</a></h3>
<pre><code class="language-cangjie">public func encrypt(input: Array&lt;Byte&gt;): Array&lt;Byte&gt;
</code></pre>
<p>功能：加密一段数据数据。</p>
<p>参数：</p>
<ul>
<li>input: Array&lt;Byte&gt; - 输入字节序列。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Array&lt;Byte&gt; - 加密后的结果。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="../../digest/digest_package_api/digest_package_exceptions.html#class-cryptoexception">CryptoException</a> - 加密失败，抛出异常。</li>
</ul>
<h3 id="func-encryptarraybyte-arraybyte"><a class="header" href="#func-encryptarraybyte-arraybyte">func encrypt(Array&lt;Byte&gt;, Array&lt;Byte&gt;)</a></h3>
<pre><code class="language-cangjie">public func encrypt(input: Array&lt;Byte&gt;, to!: Array&lt;Byte&gt;): Int64
</code></pre>
<p>功能：加密一段数据数据，指定输出数组长度会影响加解密结果。一般而言选填充模式，指定的密文数组长度不能小于明文数组长度加上一个 blockSize。</p>
<p>参数：</p>
<ul>
<li>input: Array&lt;Byte&gt; - 待进行加密的数据。</li>
<li>to!: Array&lt;Byte&gt; - 输出数组。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Int64 - 输出长度。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="../../digest/digest_package_api/digest_package_exceptions.html#class-cryptoexception">CryptoException</a> - 加密失败，抛出异常。</li>
<li>IllegalArgumentException - 当 to 的 size = 0 时，抛出异常。</li>
</ul>
<h3 id="func-encryptinputstream-outputstream"><a class="header" href="#func-encryptinputstream-outputstream">func encrypt(InputStream, OutputStream)</a></h3>
<pre><code class="language-cangjie">public func encrypt(input: InputStream, output: OutputStream)
</code></pre>
<p>功能：对输入流进行加密，一般如果数据过大无法一次对其加密，可以对数据流进行加密。</p>
<p>参数：</p>
<ul>
<li>input:InputStream  - 待加密的输入数据流。</li>
<li>output: OutputStream - 解密后的输出数据流。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="../../digest/digest_package_api/digest_package_exceptions.html#class-cryptoexception">CryptoException</a> - 加密失败，抛出异常。</li>
</ul>
<h3 id="func-decryptarraybyte"><a class="header" href="#func-decryptarraybyte">func decrypt(Array&lt;Byte&gt;)</a></h3>
<pre><code class="language-cangjie">public func decrypt(input: Array&lt;Byte&gt;): Array&lt;Byte&gt;
</code></pre>
<p>功能：解密一段数据数据。</p>
<p>参数：</p>
<ul>
<li>input: Array&lt;Byte&gt; - 输入字节序列。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Array&lt;Byte&gt; - 解密后的结果。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="../../digest/digest_package_api/digest_package_exceptions.html#class-cryptoexception">CryptoException</a> - 解密失败，抛出异常。</li>
</ul>
<h3 id="func-decryptarraybyte-arraybyte"><a class="header" href="#func-decryptarraybyte-arraybyte">func decrypt(Array&lt;Byte&gt;, Array&lt;Byte&gt;)</a></h3>
<pre><code class="language-cangjie">public func decrypt(input: Array&lt;Byte&gt;,  to!: Array&lt;Byte&gt;): Int64
</code></pre>
<p>功能：解密一段数据数据，指定输出数组长度会影响加解密结果。一般而言，指定的明文数组长度不能小于密文数组长度减去一个 blockSize。</p>
<p>参数：</p>
<ul>
<li>input: Array&lt;Byte&gt; - 待进行解密的数据。</li>
<li>to!: Array&lt;Byte&gt; - 输出数组。</li>
</ul>
<p>返回值：</p>
<ul>
<li>Int64 - 输出长度。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="../../digest/digest_package_api/digest_package_exceptions.html#class-cryptoexception">CryptoException</a> - 解密失败，抛出异常。</li>
<li>IllegalArgumentException - 当 to 的 size = 0 时，抛出异常。</li>
</ul>
<h3 id="func-decryptinputstream-outputstream"><a class="header" href="#func-decryptinputstream-outputstream">func decrypt(InputStream, OutputStream)</a></h3>
<pre><code class="language-cangjie">public func decrypt(input: InputStream, output: OutputStream)
</code></pre>
<p>功能：对输入流进行解密，一般如果数据过大无法一次对其解密，可以对数据流进行解密。</p>
<p>参数：</p>
<ul>
<li>input:InputStream  - 待解密的输入数据流。</li>
<li>output: OutputStream - 解密后的输出数据流。</li>
</ul>
<p>异常：</p>
<ul>
<li><a href="../../digest/digest_package_api/digest_package_exceptions.html#class-cryptoexception">CryptoException</a> - 解密失败，抛出异常。</li>
</ul>

                    </main>

                    <nav class="nav-wrapper" aria-label="Page navigation">
                        <!-- Mobile navigation buttons -->
                            <a rel="prev" href="../../../crypto/crypto/crypto_package_overview.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                                <i class="fa fa-angle-left"></i>
                            </a>

                            <a rel="next prefetch" href="../../../crypto/crypto/crypto_package_api/crypto_package_structs.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                                <i class="fa fa-angle-right"></i>
                            </a>

                        <div style="clear: both"></div>
                    </nav>
                </div>
            </div>

            <nav class="nav-wide-wrapper" aria-label="Page navigation">
                    <a rel="prev" href="../../../crypto/crypto/crypto_package_overview.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
                        <i class="fa fa-angle-left"></i>
                    </a>

                    <a rel="next prefetch" href="../../../crypto/crypto/crypto_package_api/crypto_package_structs.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
                        <i class="fa fa-angle-right"></i>
                    </a>
            </nav>

        </div>




        <script>
            window.playground_copyable = true;
        </script>


        <script src="../../../elasticlunr.min.js"></script>
        <script src="../../../mark.min.js"></script>
        <script src="../../../searcher.js"></script>

        <script src="../../../clipboard.min.js"></script>
        <script src="../../../cangjie-highlight-export.js"></script><script src="../../../highlight.js"></script>
        <script src="../../../book.js"></script>

        <!-- Custom JS scripts -->
        
        <script src="../../../cangjie-highlight-export.js"></script><script src="../../../highlight.js"></script>
        <script src="../../../assets/elasticlunr.js"></script>
        <script src="../../../assets/fzf.umd.js"></script>
        <script src="../../../assets/sidebar.js"></script>


    </div>
    </body>
</html>
